/**
 * 用户管理 - 编辑页面
 * @author wenchuan.li
 * @date 2018/12/12 10:35
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            detail: false,
            flag: true,
            form: {TYPE_:null},
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            // types: app.datas_('sys_user_type').removei(0),
            types: app.datas_('type'),
            statuss: app.datas_('sys_user_status').removei(0),
            rules: {
                ORG_NAME: [
                    {required: true, message: '所属部门不能为空', trigger: 'change'}
                ],
                NAME: [
                    {required: true, message: '用户名称不能为空', trigger: 'blur'}
                ],
                ACCOUNT: [
                    {required: true, message: '登录账号不能为空', trigger: 'blur'}
                ],
                PASSWORD: [
                    {required: true, message: '登录密码不能为空', trigger: 'blur'}
                ]
                // DBC: [
                //     {required: true, message: '营运中心不能为空', trigger: 'blur'}
                // ]
            }
        },
        callObj,
        pageVue,layOpenIndex
    ;

    var UserEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'user_edit'
            }, win);
        },
        editUser: function () {
            app.ajax({
                xurl: 'sys/user/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '用户信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        app.pageModule('user').reloadTableCurr();
                        //callObj.update(result.xdata);
                        layer.close(layOpenIndex);
                        //self._closeTpl();
                    }
                }
            });
        },
        addUser: function () {
            app.ajax({
                xurl: 'sys/user/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '用户信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('user').reloadTableCurr();
                        layer.close(layOpenIndex);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            layOpenIndex = obj.layOpenIndex;
            vueData.detail = false;
            vueData.flag = true;
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        ORG_ID: obj.org_id,
                        ORG_NAME: obj.org_name,
                        STATUS: '1',
                        TYPE: '',
                        SEX: '1'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                vueData.form.PASSWORD = '******';
                vueData.form.TYPE_ = callObj.data.TYPE;
                if(obj.detail){
                    vueData.detail = true;
                }else{
                    vueData.detail = false;
                }
            }
            vueData.treeData = obj.orgNodeData;
            pageVue.$refs['form'].clearValidate();

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_user', 'sys_role']);
            pageVue = new Vue({
                el: '#user_edit',
                data: vueData,
                methods: {
                    openOrgWindow: function () {
                        app.log('openOrgWindow');
                    },
                    cancel: function () {
                        if(vueData.detail){
                            layer.close(layOpenIndex);
                        }else{
                            self._closeTpl();
                        }
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addUser();
                                } else {
                                    if(vueData.flag){
                                        vueData.form.PASSWORD = '';
                                    }
                                    self.editUser();
                                }
                            }
                        });
                    },
                    handleSelect: function (item) {
                        app.log('handleSelect', item);
                    },
                    handleNodeClick: function (item) {
                        app.log('handleNodeClick', item);
                        vueData.form.ORG_ID = item.id;
                        vueData.form.ORG_NAME = item.name;
                    },
                    handleIconClick: function (ev) {
                    },
                    querySearch: function (queryString, cb) {
                        cb([{}]);
                    },
                    changeP: function () {
                        if(!vueData.isAdd) {
                            layer.confirm('是否修改密码?', {
                                title: false,
                                btn: ['确定', '取消']
                            }, function (ind) {
                                vueData.flag = false;
                                layer.close(ind);
                            }, function (inds) {
                                vueData.flag = true;
                                vueData.form.PASSWORD = '******';
                                layer.close(inds);
                            });
                        }
                    },
                    typeChange: function () {
                        if(!vueData.isAdd) {
                            layer.confirm('是否修改用户类型？如果用户类型改变,用户的所属角色会删除.', {
                                title: false,
                                btn: ['确定', '取消'],
                                cancel:function(index, layero){
                                    vueData.form.TYPE = vueData.form.TYPE_;
                                }
                            }, function (ind) {
                                vueData.form.TYPE_ = vueData.form.TYPE;
                                layer.close(ind);
                            }, function (inds) {
                                vueData.form.TYPE = vueData.form.TYPE_;
                                layer.close(inds);
                            });
                        }
                    },

                    filterNode: function (search, data) {
                        if (!search) return true;
                        return data.name.indexOf(search) !== -1;
                    },
                },
                created: function () {
                    self._setEleIsReady('user_edit');
                },
                watch:{
                'form.ORG_NAME': function (value) {
                    if(pageVue.$refs.tree != undefined) {
                        pageVue.$refs.tree.filter(vueData.form.ORG_NAME);
                    }
                },
            }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new UserEdit();

});